import { ref } from 'vue'
import { adapter, request } from '@/request'
import { auditResultByGet } from '@/core/api/priceSheet'
import type { Industry } from '@/core/interfaces/industry'
import type { ApproveStatusEnum } from '@/core/enums/status'
import type { ProductEnum } from '@/core/enums/platform'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { PriceSheet } from '@/core/interfaces/priceSheet'
import { hasPermission } from '@/utils/permission'
import { AuthEnum } from '@/core/enums/auth'
import { compact, head } from 'lodash-es'
import { IsFromHjzwEnum } from '@/core/enums/type'

interface Params {
  priceAskId: PriceSheet['id']
}

export interface AuditResult {
  approveInfo: string
  approveStatus?: ApproveStatusEnum
  approveTime: string
  approverId: number
  approverName: string
  id: number
  priceAskId: number
  userId: number
  sellerId: number
  industryCode: Industry['code']
  industryId: Industry['id']
  productId: ProductEnum
  assessmentPrice?: number
  marketPrice?: number
  comboPrice?: number
  originalAssessmentPrice: number
  hzTransferCode?: IsFromHjzwEnum
  tpCode?: number
  tpType?: string[]
  tpNotes?: string
  tpAmount?: number
}

export default function getAuditResult() {
  const service = adapter<RequestWrapper<AuditResult[]>>(request)
  const loading = ref(false)
  const response = ref<AuditResult[]>([])

  const fetch = (params: Params) => {
    const { url, method } = auditResultByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        if (hasPermission([AuthEnum.OPERATION])) {
          response.value = res.data.data
        } else {
          // 除了运营之外的角色，只能看到最新的一条审核记录
          response.value = compact([head(res.data.data)])
        }
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
